package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;

    @Override
    public PageResult pageQuery(EmpQueryParam param) {
        //1.构建分页对象
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());

        //2.调用service层方法分页查询
        //2.1 动态的构建多条件
        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(param.getName() != null && !param.getName().isEmpty(),Emp::getName, param.getName())
                .eq(param.getGender() != null,Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate, param.getBegin(), param.getEnd());

        //2.2 分页查询
        page = this.page(page,wrapper);

        //3 查询部门列表
        List<Dept> depts=  deptService.list();
        List<Emp> emps = page.getRecords();

        //4 将部门id转换成部门名称
        emps.forEach(emp -> {
            depts.forEach(dept -> {
                if (Objects.equals(emp.getDeptId(),dept.getId())){//Objects.equals避免空指针异常
                    emp.setDeptName(dept.getName());
                }
            });
        });

        return new PageResult<>(page.getTotal(),emps);
    }

    @Transactional
    @Override
    public void saveEmp(Emp emp) {
        //1.添加员工
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        this.save(emp);//调用service层方法保存数据
        //2.添加员工工作经历
        List<EmpExpr> exprList = emp.getExprList();//工作经历不止一个
                exprList.stream().forEach(expr -> {
                    expr.setEmpId(emp.getId());
                });
                empExprService.saveBatch(exprList);//调用service层方法保存数据
    }

    @Override
    public Emp searchId(Integer id) {
        Emp emp = this.getById(id);

        List<EmpExpr> list = empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id));
        emp.setExprList(list);
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        //1更新员工信息
        this.updateById(emp);
        //2批量删除员工工作经历
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        //3批量添加员工工作经历
        emp.getExprList().stream().forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        empExprService.saveBatch(emp.getExprList());

    }

    @Transactional
    @Override
    public void removeEmp(List<Integer> ids) {
        //1.删除员工
        this.removeByIds(ids);
        //2.删除员工工作经历
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }


}
